package com.kalvan.core.utils;

import com.google.common.collect.MapDifference;
import com.google.common.collect.Maps;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @author huangyueqiao
 */
public class EntityRead {
    public static void compareMap(Class class1, Class class2) {
        Map<String, String> arg0 = readEntity2Map(class1);
        Map<String, String> arg1 = readEntity2Map(class2);
        MapDifference<String, String> diff = Maps.difference(arg0, arg1);

        System.out.println(String.format("%s 类独有的属性%d个:", class1.getName(), diff.entriesOnlyOnLeft().size()));
        showMap(diff.entriesOnlyOnLeft());
        System.out.println(String.format("%s 类独有的属性%d个:", class2.getName(), diff.entriesOnlyOnRight().size()));
        showMap(diff.entriesOnlyOnRight());
        System.out.println(String.format("两个类共有属性%d个:", diff.entriesInCommon().size()));
        showMap(diff.entriesInCommon());
    }

    public static void showMap(Map<String, String> map) {
        for (String key : map.keySet()) {
            System.out.println("\t" + key);
        }
    }

    public static Map<String, String> readEntity2Map(Class<?> cls) {
        Map<String, String> hashMap = new HashMap<String, String>();
        int index = 0;
        Field[] allField = cls.getDeclaredFields();
        for (Field field : allField) {
            if (field.getName() != "serialVersionUID") {
                System.out.println("\t" + field.getName());
                hashMap.put(field.getName(), field.getName());
                index++;
            }
        }
        Class<?> superClass = cls.getSuperclass();
        Field[] allParentField = superClass.getDeclaredFields();
        for (Field field : allParentField) {
            if (field.getName() != "serialVersionUID") {
                System.out.println("\t" + field.getName());
                hashMap.put(field.getName(), field.getName());
                index++;
            }
        }
        System.out.println(cls.getName() + "统计属性，获取个数:" + index);
        return hashMap;

    }
}
